package com.michael.ims.admin.controller;


import com.michael.ims.admin.controller.io.rights.*;
import com.michael.ims.app.QTblRight;
import com.michael.ims.app.QTblRoleToRight;
import com.michael.ims.app.QTblSystem;
import com.michael.io.ReqPage;
import com.michael.io.RespData;
import com.michael.io.RespPageBody;
import com.michael.io.RespStatus;
import com.michael.plugins.PageHelper;
import com.querydsl.core.types.Projections;
import com.querydsl.sql.SQLExpressions;
import com.querydsl.sql.SQLQuery;
import com.querydsl.sql.SQLQueryFactory;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDate;

@Api(tags = "权限管理")
@RestController
@RequestMapping("/rights")
public class RightsController {

    @Autowired
    private SQLQueryFactory sqlQueryFactory;

    @ApiOperation("添加权限")
    @PostMapping("/add")
    @Transactional
    public RespStatus addRight(@RequestBody @Valid AddRightReq req) {
        QTblRight tblRight = QTblRight.tblRight;
        sqlQueryFactory.insert(tblRight)
                .set(tblRight.systemId, req.getSystemId())
                .set(tblRight.name, req.getName())
                .set(tblRight.isMenu, req.getIsMenu())
                .set(tblRight.description, req.getDescription())
                .set(tblRight.genTime, LocalDate.now())
                .execute();
        return new RespStatus();
    }

    @ApiOperation("删除权限")
    @GetMapping("/delete/{rightId}")
    @Transactional
    public RespStatus delRight(@PathVariable("rightId") Long rightId) {
        QTblRight tblRight = QTblRight.tblRight;
        sqlQueryFactory.delete(tblRight)
                .where(tblRight.id.eq(rightId))
                .execute();
        return new RespStatus();
    }

    @ApiOperation("修改权限")
    @PostMapping("/modify")
    @Transactional
    public RespStatus modifyRight(@RequestBody @Valid ModifyRightReq req) {
        QTblRight tblRight = QTblRight.tblRight;
        sqlQueryFactory.update(tblRight)
                .set(tblRight.systemId, req.getSystemId())
                .set(tblRight.name, req.getName())
                .set(tblRight.isMenu, req.getIsMenu())
                .set(tblRight.description, req.getDescription())
                .set(tblRight.genTime, LocalDate.now())
                .where(tblRight.id.eq(req.getId()))
                .execute();
        return new RespStatus();
    }

    @ApiOperation("权限列表")
    @PostMapping("/query")
    @Transactional
    public RespData<RespPageBody<QueryRightVO>> query(@RequestBody @Valid ReqPage<QueryRightCond> req) {
        PageHelper.Page<QueryRightVO> page = PageHelper.buildPage(req.getPage());

        QueryRightCond condition = req.getCondition();
        QTblRight tblRight = QTblRight.tblRight;
        QTblSystem tblSystem = QTblSystem.tblSystem;
        SQLQuery<QueryRightVO> sqlQuery = sqlQueryFactory.select(Projections.bean(QueryRightVO.class, tblRight.id, tblRight.systemId, tblSystem.name.as("systemName"),
                tblRight.name, tblRight.description,tblRight.isMenu,tblRight.genTime))
                .from(tblRight).join(tblSystem)
                .where(tblRight.systemId.eq(tblSystem.id)
                .and(tblSystem.isDelete.isFalse()));
        if (condition.getIsMenu() != null) {
            sqlQuery.where(tblRight.isMenu.eq(condition.getIsMenu()));
        }
        if (condition.getSystemId() != null) {
            sqlQuery.where(tblRight.systemId.eq(condition.getSystemId()));
        }
        if(condition.getRoleId()!=null){
            QTblRoleToRight tblRoleToRight = QTblRoleToRight.tblRoleToRight;
            sqlQuery.where(tblRight.id.notIn(
                    SQLExpressions.select(tblRoleToRight.rightId)
                            .from(tblRoleToRight)
            .where(tblRoleToRight.roleId.eq(condition.getRoleId()))));
        }
        if(StringUtils.isNotBlank(condition.getName())){
            sqlQuery.where(tblRight.name.contains(condition.getName()));
        }
        PageHelper.execute(sqlQuery);

        RespPageBody<QueryRightVO> body = new RespPageBody<>();
        body.setData(page.getData());
        body.setPage(page.getPage());

        RespData<RespPageBody<QueryRightVO>> resp = new RespData<>();
        resp.setBody(body);
        return resp;
    }
}
